package com.kamistoat.meimeistore.cart.web;

import com.kamistoat.meimeistore.cart.service.CartService;
import com.kamistoat.meimeistore.cart.vo.CartItemVo;
import com.kamistoat.meimeistore.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    CartService cartService;

    /**
     * 跳转到购物车页面
     *
     * @return
     */
    @GetMapping("/cartList.html")
    public String cartListPage(Model model, HttpServletRequest httpServletRequest) {
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart", cartVo);
        model.addAttribute("orderCheckMsg", httpServletRequest.getSession().getAttribute("orderCheckMsg"));
        httpServletRequest.getSession().removeAttribute("orderCheckMsg");
        return "cartList";
    }

    /**
     * 在商品详情页。将一件商品添加到购物车，重定向页面，不直接展示成功结果
     *
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam Long skuId, @RequestParam Integer num, @RequestParam Boolean hasStock) {
        cartService.addToCart(skuId, num, hasStock);
        return "redirect:http://cart.meimeistore.com/addToCartSuccess.html?skuId=" + skuId + "&num=" + num;
    }

    /**
     * 添加成功页面，防止用户刷新导致的重复添加
     *
     * @param skuId
     * @param num
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam Long skuId, @RequestParam Integer num, Model model) {
        CartItemVo cartItemVo = cartService.getCartItem(skuId, num);
        model.addAttribute("cartItem", cartItemVo);
        return "success";
    }

    /**
     * 勾选购物车商品接口方法
     *
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam Long skuId, @RequestParam Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.meimeistore.com/cartList.html";
    }

    /**
     * 改变商品数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam Long skuId, @RequestParam Integer num) {
        cartService.countItem(skuId, num);
        return "redirect:http://cart.meimeistore.com/cartList.html";
    }

    /**
     * 删除某个商品
     *
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.meimeistore.com/cartList.html";
    }

    /**
     * 远程接口
     * 获取用户购物车所有被选中的商品项
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getOrderCartChecked")
    public List<CartItemVo> getOrderCartChecked() {
        List<CartItemVo> cartItemVoList = cartService.getOrderCartChecked();
        return cartItemVoList;
    }

}
